package com.apes.scm.masterdata.product.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.fileserver.File;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 功能：商品类目（配件名称）
 *
 * @author HuJianlin
 * @create 2018-03-15 16:00
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = Category.CONST_TABLE_NAME)
public class Category extends Model {
    public static final String CONST_TABLE_NAME = "mas_category";

    @Id
    @Column(name = "id", length = 6)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "商品类目编码", padding = 6)
    @Comment("商品类目编码")
    private String id;  //商品类目编码

    @Column(name = "name", nullable = false, length = 120)
    @Comment("商品类目名称")
    private String name; //商品类目名称

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "classify_id", referencedColumnName = "id")
    @Comment("商品分类编码")
    private Classify classify;  //商品分类

    @Column(name = "valid", nullable = false, columnDefinition = "number(1,0) default 1")
    @Comment("有效标记")
    private boolean valid = true; //有效

    @Column(name = "match_color", columnDefinition = "number(1,0)")
    @Comment("是否匹配颜色")
    private boolean matchColor = true;
    @Column(name = "match_product", columnDefinition = "number(1,0)")
    @Comment("是否匹配商品")
    private boolean matchCProduct = true;

    @Name
    @File
    @Comment("类目照片")
    @Basic(fetch = FetchType.LAZY)
    @Column(name = "category_Photos", length = 4000)
    private String categoryPhotos;

    @OneToMany(mappedBy = "category", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<CategoryTag> categoryTags = new ArrayList<>(); //标签

    public List<CategoryTag> getCategoryTags() {
        if (categoryTags == null) categoryTags = new ArrayList<>();
        for (CategoryTag item : categoryTags) {
            item.setCategory(this);
        }
        return categoryTags;
    }

    @Transient
    private String tagName;

    @OneToMany(mappedBy = "categoryId", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<CategoryWorkHours> categoryWorkHoursArrayList = new ArrayList<>();

    public List<CategoryWorkHours> getCategoryWorkHoursArrayList() {
        if (categoryWorkHoursArrayList == null) categoryWorkHoursArrayList = new ArrayList<>();
        for (CategoryWorkHours workHours : categoryWorkHoursArrayList) {
            workHours.setCategoryId(this);
        }
        return categoryWorkHoursArrayList;
    }


}
